﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GeoUtils
{
    public class RewriteAddress
    {
        HashSet<string> setAdminSuffix;
        HashSet<string> setRoadSuffix;

        public void Initialize()
        {
            setAdminSuffix = new HashSet<string>();
            setRoadSuffix = new HashSet<string>();

            setAdminSuffix.Add("省");
            setAdminSuffix.Add("市");
            setAdminSuffix.Add("区");
            setAdminSuffix.Add("县");
            setAdminSuffix.Add("乡");
            setAdminSuffix.Add("镇");
            setAdminSuffix.Add("村");
            setAdminSuffix.Add("自治区");
            setAdminSuffix.Add("回族自治区");
            setAdminSuffix.Add("壮族自治区");
            setAdminSuffix.Add("维吾尔族自治区");
            setAdminSuffix.Add("直辖市");
            setAdminSuffix.Add("特别行政区");
            setAdminSuffix.Add("行政区");
            setAdminSuffix.Add("特区");
            setAdminSuffix.Add("地区");
            setAdminSuffix.Add("自治旗");
            setAdminSuffix.Add("自治县");
            setAdminSuffix.Add("新村");
            setAdminSuffix.Add("辖区");
            setAdminSuffix.Add("市辖区");
            
            setRoadSuffix.Add("大街");
            setRoadSuffix.Add("胡同");
            setRoadSuffix.Add("路");
            setRoadSuffix.Add("街道");
            setRoadSuffix.Add("东路");
            setRoadSuffix.Add("西路");
            setRoadSuffix.Add("南路");
            setRoadSuffix.Add("北路");
            setRoadSuffix.Add("中路");
            setRoadSuffix.Add("东街");
            setRoadSuffix.Add("西街");
            setRoadSuffix.Add("南街");
            setRoadSuffix.Add("北街");
            setRoadSuffix.Add("中街");
            setRoadSuffix.Add("大道");

        }

        private List<string> RemoveSuffix(string strName, HashSet<string> setSuffix)
        {
            List<string> rstList = new List<string>();
            for (int i = 2; i < strName.Length; i++)
            {
                string strSuffix = strName.Substring(i);
                if (setSuffix.Contains(strSuffix) == true)
                {
                    rstList.Add(strName.Substring(0, i));
                    return rstList;
                }
            }
            return rstList;
        }

        private List<string> ExtAdminName(string strName)
        {
            List<string> rst = new List<string>();
            if (strName.EndsWith("市") == true)
            {
                if (strName.Length > 2)
                {
                    rst.Add(strName + "区");
                }
                else
                {
                    rst.Add(strName + "市区");
                }
            }

            return rst;
        }

        public List<string> RewriteRoadName(string strName)
        {
            List<string> rstList = new List<string>();
            List<string> rstRemoveSuffixList = RemoveSuffix(strName, setRoadSuffix);
            if (rstRemoveSuffixList.Count > 0)
            {
                rstList.AddRange(rstRemoveSuffixList);
            }

            return rstList;
        }

        public List<string> RewriteAdminName(string strName)
        {
            List<string> rstList = new List<string>();
            List<string> rstRemoveSuffixList = RemoveSuffix(strName, setAdminSuffix);
            if (rstRemoveSuffixList.Count > 0)
            {
                rstList.AddRange(rstRemoveSuffixList);
            }

            List<string> rstExtList = ExtAdminName(strName);

            rstList.AddRange(rstExtList);

            return rstList;
        }

        public string AddNumberAtSuffix(string strAddress)
        {
            if (strAddress.Length == 0)
            {
                return null;
            }

            if (strAddress[strAddress.Length - 1] >= '0' &&
                strAddress[strAddress.Length - 1] <= '9')
            {
                strAddress = strAddress + "号";

                return strAddress;
            }

            return null;
        }

        public List<string> RewriteAddressName(string strName)
        {
            List<string> rstList = new List<string>();
            if (strName.Length > 0)
            {
                string strAddNumberAtSuffix;
                strAddNumberAtSuffix = AddNumberAtSuffix(strName);
                if (strAddNumberAtSuffix != null)
                {
                    rstList.Add(strAddNumberAtSuffix);
                }
            }

            return rstList;
        }
    }
}
